﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SMSTemplate.aspx.cs" Inherits="CRMWeb.Pages.Admin.SMSTemplate" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>SMS Template</title>
    <link rel="stylesheet" id="newtheme" type="text/css" href="/EasyUI/Themes/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="/EasyUI/Themes/icon.css" />
    <link rel="stylesheet" type="text/css" href="/Css/CrmStyle.css" />
    <script type="text/javascript" src="/Scripts/jquery-1.8.0.min.js"></script>
    <script type="text/javascript" src="/EasyUI/jquery.easyui.min.js"></script>
</head>
<body>
    <div class="easyui-panel" title="SMS Template" id="pnlMain" style="width: auto; height: auto">

        <div style="float: left; width: 300px">
            <div id="pnltree" class="easyui-panel" title="SMS Category">
                <ul id="tvSMSCategory" class="easyui-tree" animate="true" data-options="url:'/WebServices/SMSTemplate.ashx?method=DisplayCategoryList&Company=' + getQuerystring('Company', ''),required:true,
                    onSelect:function(node){
                       $('#dgSmsList').datagrid({
                        url: '/WebServices/SMSTemplate.ashx?method=GetSMSDataList&Company=' + getQuerystring('Company', '')+'&CategoryID='+node.id
                        });
                    },
                    onLoadSuccess:function(){
                     var rootnode = $('#tvSMSCategory').tree('getRoot');
                            if (rootnode == null) {
                                $('#divSMSTemplate').hide();
                                $.messager.alert('Morgan CRM', 'No SMS Template available ' + usrCompany, 'info');
                            }
                            else {
                                $('#divSMSTemplate').show();
                                $('#tvSMSCategory').tree('select', rootnode.target);
                            }
                            $('#dgSmsList').datagrid('resize', {
                            width: window.innerWidth - 310,
                            height: window.innerHeight - 30
                            }); 
                    }">
                </ul>
            </div>
        </div>

        <div id="divSMSTemplate" style="width: 450px">
            <table id="dgSmsList"
                toolbar="#toolbarList" pagination="true" data-options="pageSize:20"
                rownumbers="true" fitcolumns="true" singleselect="true" autorowheight="true">
                <thead>
                    <tr>
                        <th field="SMSTemplate" width="100" align="center">TemplateName</th>
                        <th field="SMSCompany" width="150" align="center">Company</th>
                    </tr>
                </thead>
            </table>
            <div id="toolbarList">
                <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-add" plain="true" onclick="newSMSTemplate()">New</a>
                <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-edit" plain="true" onclick="editSMSTemplate()">Edit</a>
                <a id="lnkDelete" href="javascript:void(0)" class="easyui-linkbutton" onclick="openDelSMSPopup()" plain="true" iconcls="icon-remove" title="Delete Notice">Delete</a>
            </div>

        </div>
    </div>



    <div id="dlgSmsTemp" class="easyui-dialog" style="width: 600px; height: 460px; padding: -3px"
        closed="true" buttons="#dlg-buttons">
        <form id="fmSmsTemp" method="post" novalidate>

            <div class="fitem" style="margin-top: 9px">
                <label style="width: 120px; margin-left: 20px">Select Template:</label>
                <span id="spnddlSMSTemplate">
                    <input class="easyui-combotree" value="" id="ddlSMSTemplate" style="width: 300px;"
                        data-options="url:'/WebServices/SMSTemplate.ashx?method=DisplayTemplateNode&Company=' + getQuerystring('Company', ''),id: 'CategoryId',
		text: 'SMSTemplate',required:true, onSelect:selectSMSTemplate " />
                </span>
                <input id="txtSMSTemplate" class="easyui-validatebox" required="true" />

            </div>


            <div class="fitem">
                <label style="width: 120px; margin-left: 20px">Company:</label>
                <input class="easyui-combobox" id="cmbSMSRoles" style="width: 300px;"
                    data-options="panelWidth:300, 
                            editable:false,
                     multiple:true,
                        valueField: 'MemberGroupName',  
                        textField: 'MemberGroupName',
                        url: '/WebServices/Notice.ashx?method=BindAllRole',
                    selected:true" />
            </div>


            <div class="fitem">
                <label style="width: 120px; margin-left: 20px">Field:</label>
                <input class="easyui-combobox" id="ddlField" name="Field" style="width: 300px;" data-options="panelHeight:'300',url:'/WebServices/SMSTemplate.ashx?method=getColumnName1',
				valueField:'COLUMN_NAME',
				textField:'COLUMN_NAME',
                onSelect: function(rec){ 
                    var caretPos = document.getElementById('txtTemplateSMSFormat').selectionStart;
                    var textAreaTxt = jQuery('#txtTemplateSMSFormat').val();
                    var txtToAdd = '%'+rec.COLUMN_NAME+'%';
                    jQuery('#txtTemplateSMSFormat').val(textAreaTxt.substring(0, caretPos) + txtToAdd + textAreaTxt.substring(caretPos) );
                }" />
            </div>
            <div class="fitem">
                <span id="spnCategory" style="display: none">
                    <label style="width: 120px; margin-left: 20px">Category:</label>
                    <select class="easyui-combotree" id="cmbCategory" style="width: 420px;"
                        data-options="url:'/WebServices/SMSTemplate.ashx?method=DisplayCategoryList&Company=' + getQuerystring('Company', ''),required:true">
                    </select>
                </span>
            </div>
            <div class="fitem">
                <label style="width: 120px; margin-left: 20px">Operator:</label>
                <select id="cmbOperatorSMS" data-options="panelHeight:'auto',onSelect: function(rec){
                    var caretPos = document.getElementById('txtTemplateSMSFormat').selectionStart;
                    var textAreaTxt = jQuery('#txtTemplateSMSFormat').val();
                    var txtToAdd = $('#cmbOperatorSMS').combogrid('getValue');
                    jQuery('#txtTemplateSMSFormat').val(textAreaTxt.substring(0, caretPos) + txtToAdd + textAreaTxt.substring(caretPos) );
                    }"
                    class="easyui-combobox" name="Operator" style="width: 110px;">
                    <option value="%OperatorName%">Operator Name</option>
                </select>
            </div>
            <input id="txtOprSMS" class="easyui-validatebox" required="true" hidden="true" />

            <div id="divSmsText" class="fitem">
                <label style="vertical-align: top; width: 120px; margin-left: 20px">Description:</label>
                <textarea id="txtTemplateSMSFormat" name="SMSTemplateDescription" class="easyui-validatebox" style="width: 70%; height: 230px"></textarea>
            </div>
            <input type="hidden" id="hdnSmsRole" runat="server" />
        </form>
    </div>
    <div id="dlg-buttons">
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-cancel" onclick="javascript:$('#dlgSmsTemp').dialog('close')" style="float: left">Cancel</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" iconcls="icon-ok" onclick="SaveSMSTemplate()">Save</a>
    </div>
    <div id="dlgDelSMSList" class="easyui-dialog" title="Confirm" style="width: 300px; height: 160px" closed="true" resizable="false" modal="true">
        <h4 style="text-align: center">Are you sure want to delete this template?</h4>
        <div class="fitem" style="text-align: center; padding: 10px; margin-top: 10px">
            <a id="lnkDelSmsList" href="javascript:void(0)" class="easyui-linkbutton" onclick="deleteSmsList()" disabled="true">OK</a>
            <a id="lnkCancleSmsList" href="javascript:void(0)" class="easyui-linkbutton" onclick="$('#dlgDelSMSList').dialog('close')" disabled="true">Cancel</a>
            <span id="spnanimate">
                <img id="deleteProgress" style="margin-left: 20px" src="../../Media/loading.gif" />
                <label id="lbltimeOutn" style="margin-left: 20px">5</label>
            </span>
        </div>
    </div>

</body>
<script type="text/javascript">
    var smsRoleValue = '';
    var setRoles = '';

    //Change Theme:
    function getQuerystring(key, defaultValue) {
        if (defaultValue == null) defaultValue = "";
        key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
        var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
        var qs = regex.exec(window.location.href);
        if (qs == null)
            return defaultValue;
        else {
            return qs[1];
        }
    }


    $(document).ready(function () {
        $('#newtheme').attr('href', '/EasyUI/Themes/' + getQuerystring('Theme', '') + '/easyui.css');
    });


    $('#dlgDelSMSList').dialog({
        onOpen: function () {
            startCounter(5);
        },
        onClose: function () {
            $('#lnkDelSmsList').linkbutton('disable');
            $('#lnkCancleSmsList').linkbutton('disable');
            $('#spnanimate').show();
            $('#lbltimeOutn').html('');
        }
    });
    $('#dgSmsList').datagrid({
        onDblClickRow: function (index, rowdata) {
            editSMSTemplate();
        }
    });

    function startCounter(limit) {
        setTimeout(function () {
            if (limit == 0) {
                $('#lnkDelSmsList').linkbutton('enable');
                $('#lnkCancleSmsList').linkbutton('enable');
                $('#spnanimate').hide();

            }
            else {
                $('#lbltimeOutn').html(limit);
                startCounter(limit - 1);
            }
        }, 1000);
    }

    $(document).ready(function () {

        $('#ddlSMSTemplate').combotree({
            onClick: function (node) {
                if (!$('#ddlSMSTemplate').tree('isLeaf', node.target)) {
                    $('#ddlSMSTemplate').combotree('setValues', '');
                    var t = $('#ddlSMSTemplate').combotree('tree');
                    t.tree('toggle', node.target);
                    var p = $('#ddlSMSTemplate').combotree('panel');
                    p.panel('open');
                }
            }
        });
        $('#cmbCategory').combotree({
            onClick: function (node) {
                if (!$('#cmbCategory').tree('isLeaf', node.target)) {
                    $('#cmbCategory').combotree('setValues', '');
                    var t = $('#cmbCategory').combotree('tree');
                    t.tree('toggle', node.target);
                    var p = $('#cmbCategory').combotree('panel');
                    p.panel('open');
                }
            }
        });

        $('#pnltree').panel('resize', {
            height: window.innerHeight - 30
        });

        $('#txtSMSTemplate').hide();
        $('#lnkcancelSMSTemplate').hide();

        $('#ddlField').combobox('textbox').bind('click', function () {
            $('#ddlField').combobox('showPanel');
        });

        $('#cmbSMSRoles').combobox('textbox').bind('click', function () {
            $('#cmbSMSRoles').combobox('showPanel');
        });

        $('#ddlSMSTemplate').combobox('textbox').bind('click', function () {
            $('#ddlSMSTemplate').combobox('showPanel');
        });
        $('#cmbCategory').combobox('textbox').bind('click', function () {
            $('#cmbCategory').combobox('showPanel');
        });
        $('#cmbOperatorSMS').combobox('textbox').bind('click', function () {
            $('#cmbOperatorSMS').combobox('showPanel');
        });
        $('#lnkdeleteSMSTemplate').hide();
    });
    $(document).keydown(function (event) {
        if ((event.which == 83 || event.which == 115) && (event.ctrlKey || event.metaKey) || (event.which == 19)) {
            event.preventDefault();
            // do stuff - ctrl + s
            SaveSMSTemplate();
            return false;
        }
        return true;
    });
    window.onresize = function () {
        $('#dlgSmsTemp').panel('resize', {
            width: window.innerWidth,
            height: window.innerHeight
        });
        $('#pnlMain').panel('resize', {
            width: window.innerWidth - 2,
            height: window.innerHeight - 2
        });

        $('#pnltree').panel('resize', {
            height: window.innerHeight - 35
        });

        $('#dgSmsList').datagrid('resize', {
            width: window.innerWidth - 315,
            height: window.innerHeight - 35
        });

        $('#txtTemplateSMSFormat').css('height', (window.innerHeight / 1.5) - 50);
    }


    function selectSMSTemplate(node) {

        var currentTemplateId = node.id;
        if (currentTemplateId == -1) {
            $.messager.alert('Morgan CRM', 'Please select Template Item ... !', 'warning');
        }
        else {
            $('#lnkdeleteSMSTemplate').show();
            $.ajax({
                url: "/WebServices/SMSTemplate.ashx?method=GetSMSTemplateContentById",
                contentType: "application/json; charset=utf-8",
                data: { smsTemplateId: currentTemplateId },
                success: function (data) {
                    $.each(eval(data.replace(/[\r\n]/, '')), function (i, result) {
                        $('#cmbCategory').combotree('setValue', result.CategoryId);
                        $('#txtTemplateSMSFormat').val(result.TemplateFormat);
                        var defSMSRole = result.SMSCompany;
                        if (defSMSRole != null) {
                            var setSMSRole = defSMSRole.split(',');
                            $('#cmbSMSRoles').combobox('setValues', setSMSRole);
                        }
                        else {
                            $('#cmbSMSRoles').combobox('setValues', '');
                        }



                    });
                }
            });
        }
    }
    function newSMSTemplate() {
        $('#dlgSmsTemp').dialog('open');

        $('#dlgSmsTemp').panel('setTitle', 'Create New SMS Template');
        $('#txtSMSTemplate').val('');
        $('#txtTemplateSMSFormat').val('');
        $('#ddlSMSTemplate').combotree('reload');
        $('#ddlSMSTemplate').combotree('setValue', '');
        $('#cmbSMSRoles').combobox({
            onLoadSuccess: function (data) {
                $(this).combobox('select', data[0]['MemberGroupName']);
            },
        });



        $('#cmbCategory').combotree('reload');
        $('#ddlField').combobox('setValue', '');

        $('#txtSMSTemplate').show();
        $('#spnddlSMSTemplate').hide();
        $('#spnCategory').show();
        $('#lnkdeleteSMSTemplate').hide();
        $('#lnkcancelSMSTemplate').show();
        $('#lnknewSMSTemplate').hide();
    }

    function editSMSTemplate() {

        var row = $('#dgSmsList').datagrid('getSelected');
        if (row) {
            $('#dlgSmsTemp').dialog('open');
            $('#lnknewSMSTemplate').show();
            $('#txtSMSTemplate').hide();
            $('#spnddlSMSTemplate').show();
            $('#dlgSmsTemp').panel('setTitle', 'Edit SMS Template');
            var currentTemplateId = row.SMSTemplateID;
            if (currentTemplateId == -1) {
                $.messager.alert('Morgan CRM', 'Please select Template Item ... !', 'warning');
            }
            else {
                $.ajax({
                    url: "/WebServices/SMSTemplate.ashx?method=GetSMSTemplateContentById",
                    contentType: "application/json; charset=utf-8",
                    data: { smsTemplateId: currentTemplateId },
                    success: function (data) {
                        $.each(eval(data.replace(/[\r\n]/, '')), function (i, result) {
                            $('#cmbCategory').combotree('setValue', result.CategoryId);



                            $('#ddlSMSTemplate').combotree('setValue', row.SMSTemplateID);

                            $('#txtTemplateSMSFormat').val(result.TemplateFormat);
                            var defSMSRole = result.SMSCompany;
                            if (defSMSRole != null) {
                                var setSMSRole = defSMSRole.split(',');
                                $('#cmbSMSRoles').combobox('setValues', setSMSRole);
                            }
                            else {
                                $('#cmbSMSRoles').combobox('setValues', '');
                            }



                        });
                    }
                });
            }

        }



    }

    function cancelSMSTemplate() {
        $('#dlgSmsTemp').panel('setTitle', 'SMS Template');
        $('#dgSmsList').datagrid('reload');
        $('#dlgSmsTemp').dialog('close');
        $('#txtSMSTemplate').val('');
        $('#txtTemplateSMSFormat').val('');
        $('#ddlSMSTemplate').combotree('reload');
        $('#ddlSMSTemplate').combotree('setValue', '');
        $('#cmbCategory').combotree('setValue', '');
        $('#cmbSMSRoles').combobox('setValues', '');
        $('#ddlField').combobox('setValue', '');
        $('#txtSMSTemplate').hide();
        $('#spnddlSMSTemplate').show();
        $('#spnCategory').hide();

        $('#lnkcancelSMSTemplate').hide();
        $('#lnknewSMSTemplate').show();
        $('#lnkdeleteSMSTemplate').hide();
    }


    function openDelSMSPopup() {
        var row = $('#dgSmsList').datagrid('getSelected');
        if (row) {
            $('#dlgDelSMSList').dialog('open');

        }
    }

    function deleteSMSTemplate() {
        var t = $('#ddlSMSTemplate').combotree('tree');	// get the tree object
        var templatNode = t.tree('getSelected');

        var SMSTemplateID = templatNode.id;
        if (SMSTemplateID != '') {
            $.ajax({
                url: '/WebServices/SMSTemplate.ashx',
                type: 'POST',
                data: {
                    method: 'SMSTemplateDelete',
                    args: { SMSTemplateID: SMSTemplateID }
                },
                success: function (data) {
                    $.messager.alert('Morgan CRM', 'SMS Template Successfully Deleted');
                    cancelSMSTemplate();

                }
            });
        }
        else {
            $.messager.alert('Morgan CRM', 'Please select value to delete');
        }
    }




    function IsValidate() {


        var flag = true;
        if ($('#txtSMSTemplate').is(':visible') == true && $('#txtSMSTemplate').val() == '') {
            $.messager.alert('Morgan CRM', 'Template name is required.', 'warning');
            return flag = false;
        }

        var trCat = $('#cmbCategory').combotree('tree');	// get the tree object
        var CategorynodeTr = trCat.tree('getSelected');

        if (CategorynodeTr == null) {
            $.messager.alert('Morgan CRM', 'Please select category... !', 'warning');
            return flag = false;
        }


        return flag;
    }

    function SaveSMSTemplate() {
        if (IsValidate()) {

            var SMSTemplateID = '';
            var tr = $('#cmbCategory').combotree('tree');	// get the tree object
            var Categorynode = tr.tree('getSelected');            // get selected node

            var t = $('#ddlSMSTemplate').combotree('tree');	// get the tree object
            var templatNode = t.tree('getSelected');
            if (templatNode != null)
                SMSTemplateID = templatNode.id;
            var SMSTemplate = $('#txtSMSTemplate').val();
            var SMSTemplateFormat = $('#txtTemplateSMSFormat').val();
            var SMSCompany = $('#cmbSMSRoles').combobox('getText');
            //if (SMSTemplateID == Categorynode.id)
            //{
            // $.messager.alert('Morgan CRM', 'SMS Template updated successfully.');
            //}
            if (SMSTemplateID != '') {

                $.ajax({
                    url: '/WebServices/SMSTemplate.ashx',
                    type: 'POST',
                    data: {
                        method: 'SMSTemplateUpdate',
                        args: { SMSTemplateID: SMSTemplateID, TemplateFormat: SMSTemplateFormat, CategoryID: Categorynode.id, SMSCompany: SMSCompany }
                    },
                    success: function (data) {
                        if (data == 1) {
                            $.messager.alert('Morgan CRM', 'SMS Template updated successfully.');
                            cancelSMSTemplate();
                        }
                        else
                            $.messager.alert('Morgan CRM', 'SMSTemplate already exist !');
                    }
                });

            }
            else {
                if (SMSTemplate != '') {
                    $.ajax({
                        url: '/WebServices/SMSTemplate.ashx',
                        type: 'POST',
                        data: {
                            method: 'SMSTemplateAdd',
                            args: { SMSTemplate: SMSTemplate, TemplateFormat: SMSTemplateFormat, CategoryID: Categorynode.id, SMSCompany: SMSCompany }
                        },
                        success: function (data) {
                            if (data == 1) {
                                $.messager.alert('Morgan CRM', 'SMS Template added successfully ');
                                cancelSMSTemplate();
                            }
                            else
                                $.messager.alert('Morgan CRM', 'SMS Template already exist !');
                        }
                    });
                }
            }
        }

    }

    //delete list data:
    function deleteSmsList() {
        var row = $('#dgSmsList').datagrid('getSelected');
        if (row) {
            $.ajax({
                url: '/WebServices/SMSTemplate.ashx',
                type: 'POST',
                data: {
                    method: 'DeleteSMSData',
                    args: { SMSTemplateID: row.SMSTemplateID }

                },
                success: function (data) {
                    $('#dgSmsList').datagrid('reload');
                    $('#dlgDelSMSList').dialog('close');
                }
            });
        }
    }


</script>
</html>
